package com.demo.modules.mq.rabbit.configuration;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author MyPC
 */
@Configuration
public class DeadConfiguration {

    /**
     * 超时时间
     */
    private final Integer TIME_OUT = 1000 * 5;

    //队列
    public static final String FORMAL_QUEUE = "formal.queue11";
    public static final String DEAD_QUEUE = "dead.queue12";

    //交换机
    public static final String FORMAL_EXCHANGE = "formal.exchange";
    public static final String DEAD_EXCHANGE = "dead.exchange";

    //路由key
    public static final String FORMAL_ROUNTE_KEY = "formal_rounte_key";
    public static final String DEAD_ROUNTE_KEY = "dead_route_key";


    /**
     * 声明一个direct类型的普通交换机
     *
     * @return
     */
    @Bean
    public DirectExchange directFormalExchange() {
        return new DirectExchange(FORMAL_EXCHANGE);
    }

    /**
     * 生命普通队列
     *
     * @return
     */
    @Bean
    public Queue getFormalQueue() {
        Map<String, Object> map = new HashMap<>();
        //设置队列最大长度
        map.put("x-max-length", 5);
        //设置死信队列交换机
        map.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        //设置死信队列路由key
        map.put("x-dead-letter-routing-key", DEAD_ROUNTE_KEY);
        //设置ttl
        map.put("x-message-ttl", TIME_OUT);
        return new Queue(FORMAL_QUEUE, true, false, false, map);
    }

    /**
     * 绑定普通队列和交换机
     *
     * @return
     */
    @Bean
    public Binding bingFormalQueue() {
        return new Binding(FORMAL_QUEUE, Binding.DestinationType.QUEUE, FORMAL_EXCHANGE, FORMAL_ROUNTE_KEY, null);
    }


    /**
     * 声明一个direct类型的死信交换机
     *
     * @return
     */
    @Bean
    public DirectExchange directDeadExchange() {
        return new DirectExchange(DEAD_EXCHANGE);
    }

    /**
     * 声明死信队列
     *
     * @return
     */
    @Bean
    public Queue getDeadQueue() {
        return new Queue(DEAD_QUEUE, true, false, false, null);
    }

    /**
     * 将死信队列和交换机绑定
     *
     * @return
     */
    @Bean
    public Binding bingDeadQueue() {
        return new Binding(DEAD_QUEUE, Binding.DestinationType.QUEUE, DEAD_EXCHANGE, DEAD_ROUNTE_KEY, null);
    }
}
